// pages/appointment/appointment.js
var request = require("../../utils/request.js")
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: app.globalData.statusBarHeight,
        navH: app.globalData.navHeight,
        query: {
            position: '',
            keyWord: '',
            order: '1'

        },
        list: [],
        info:"",
        info1:'',
        s:"0",
        m:"0"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        //获取地理位置
        wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
    },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage (res) {
    return {
      title: '迪佳极视',
      path: 'pages/index/index',
      // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete:function(res){
        // 不管成功失败都会执行
      }
    }
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.toastDialog = this.selectComponent("#toastDialog");//此处id和页面的 wxml中的id一致
        this.getPosition()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getPosition: function () {
        var that = this
        wx.getLocation({
            type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function (res) {
                that.setData({ 'query.position': res.longitude + ',' + res.latitude })
                that.queryClinicInfoList()

            }
        })
    },
    getKeyWord: function (e) {
        this.setData({ 'query.keyWord': e.detail.value })
    },
    queryClinicInfoList: function (e) {
        let that = this
        that.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框
        if (e != undefined && e.currentTarget.dataset.order != undefined) {
            that.setData({ 'query.order': e.currentTarget.dataset.order })
        }
        console.log(that.data.query)
        request.postData('xcx/clinicList', that.data.query, function (res) {
            // success
            that.toastDialog.hideDialog(); //关闭loading提示框
            console.log(res)
            if (res.code == '0') {
                console.log(res);
                if(res.list.length == 0){
                    that.setData({
                        list:res.list,
                        s: "1",
                        m:'1',
                        info: "暂无数据~"
                      })
                }else{
                    for (var i = 0; i < res.list.length; i++) {
                        let isHalf = false; //用来判断最后一颗星是否是满星
                        let starInfo = []; //存放星星，用来循环星星
                        let starNum = 0;  //向上取整，用来判断有几颗星星
                        if (Math.floor(res.list[i].evaluate) === res.list[i].evaluate) {
                            isHalf = false
                            starNum = res.list[i].evaluate;
                        } else {
                            isHalf = true
                            starNum = Math.ceil(res.list[i].evaluate); //向上取整，用来判断有几颗星星
                        }
                        for (var j = 0; j < starNum; j++) {
                            let obj = {}
                            if (isHalf && j == starNum - 1) {
                                obj = { star: "../../images/icon/star_half.png" }
                            } else {
                                obj = { star: "../../images/icon/star.png" }
                            }
                            starInfo.push(obj)
                        }
                        res.list[i].starInfo = starInfo
                    }
                    that.setData({
                         list: res.list,
                         m:'0',
                         s:'0'
                         })
                }
               
               
                //console.log(that.data.list)
            } else {
                that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
            }

        }, function (res) {
            // error
            that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
        });
    },
    gotoDetail: function (e) {
        //console.log(e);
        wx.navigateTo({
            url: '../appoinmentDetails/appoinmentDetails?cid=' + e.currentTarget.dataset.id
        })
    }
})